package com.one.consumer.user.ui.binder;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.MyToast;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.consumer.R;
import com.one.consumer.user.model.UserModel;
import com.one.consumer.user.model.extra.AddressExtra;
import com.one.consumer.user.model.item.AddressItem;
import com.one.consumer.user.model.item.LookUpItem;

import java.util.List;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/28 4:15 PM
 * description ：
 **/
public class AddressBinder extends BaseItemBinder<AddressItem> {

    public AddressBinder(AddressClickListener listener) {
        super(R.layout.item_address);
        this.addressClickListener = listener;
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull AddressItem item) {
        holder.setText(R.id.tv_contact, item.getAddress_name());
        holder.setText(R.id.tv_contact_phone, item.getAddress_mobile());
        holder.setText(R.id.tv_address, item.getAddress());

        CheckBox checkBox = holder.getView(R.id.cb_address);
        if (item.getIs_default()) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

        // 编辑
        holder.getView(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.ADD_ADDRESS,
                        new AddressExtra(AddressExtra.UPDATE, item));
            }
        });

        // 删除
        holder.getView(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(item);
            }
        });

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && !item.getIs_default()) {
                    // 选中
                    changeCheck(item);
                }
            }
        });

        holder.getView(R.id.ll_address).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addressClickListener.onAddressClick(item);
            }
        });
    }

    /**
     * 切换选中
     *
     * @param item
     */
    private void changeCheck(AddressItem item) {
        new UserModel((BaseActivity) mContext).defaultAddress(item.getAddress_id() + "",
                new ObserverOnResultListener<Object>() {
                    @Override
                    public void onResult(Object o) {
                        Toaster.showLongToast("修改默认地址成功！", MyToast.SUCCESS);
                        changeUI(item);
                    }
                });
    }

    /**
     * 更新显示
     */
    private void changeUI(AddressItem item) {
        List<AddressItem> datas = (List<AddressItem>) getAdapter().getItems();
        for (AddressItem address : datas) {
            if (address.getAddress_id() == item.getAddress_id()) {
                address.setIs_default("1");
            } else {
                address.setIs_default("0");
            }
        }
        getAdapter().notifyDataSetChanged();
    }


    private AddressClickListener addressClickListener;

    public interface AddressClickListener {
        void onAddressClick(AddressItem item);

        void onDelete(AddressItem item);
    }

    private void delete(AddressItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("确认删除吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                doDelete(item);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.show();
    }

    private void doDelete(AddressItem item) {
        new UserModel((BaseActivity) mContext).deleteAddress(item.getAddress_id() + "",
                new ObserverOnResultListener<Object>() {
                    @Override
                    public void onResult(Object o) {
                        // 删除成功
                        Toaster.showLongToast("删除成功", MyToast.SUCCESS);
                        addressClickListener.onDelete(item);
                    }
                });
    }

}
